home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_085 / vmore / vmore.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  267 lines

  1. /************************************************************
  2.   Vmore.c  By: Stephen Vermeulen
  3.  
  4.   Copyright (C) 1987 By Stephen Vermeulen
  5.  
  6.   Syntax:
  7.            Vmore filename [filename [filename [...]]]
  8.  
  9.   Yet another text display routine.
  10. ************************************************************/
  11.  
  12. #include <intuition/intuition.h>
  13. #include <workbench/startup.h>
  14. #include <exec/memory.h>
  15. #include <stdio.h>
  16. #include <functions.h>
  17.  
  18. #define NO_INTUITION   1
  19. #define NO_GRAPHICS    2
  20.  
  21. extern struct WBStartup *WBenchMsg;
  22.  
  23. struct IntuitionBase *IntuitionBase;
  24. struct GfxBase *GfxBase;
  25.  
  26. extern char *malloc();
  27.  
  28. /************************************************************
  29.   The following routine just opens the libraries
  30. ************************************************************/
  31.  
  32. short OpenLibs()
  33. {
  34.   short flags; /* any libraries that do not open get recorded here */
  35.  
  36.   flags = 0;
  37.   IntuitionBase = (struct IntuitionBase *)
  38.                   OpenLibrary("intuition.library", 0L);
  39.   GfxBase =       (struct GfxBase *)
  40.                   OpenLibrary("graphics.library", 0L);
  41.   if (!IntuitionBase) flags |= NO_INTUITION;
  42.   if (!GfxBase) flags |= NO_GRAPHICS;
  43.   return(flags);
  44. }
  45.  
  46. void CloseLibs(flags)
  47. short flags;
  48. {
  49.   if (!(flags & NO_INTUITION)) CloseLibrary(IntuitionBase);
  50.   if (!(flags & NO_GRAPHICS))  CloseLibrary(GfxBase);
  51. }
  52.  
  53. struct NewWindow nw =
  54. {
  55.   0, 1, 640, 199,
  56.   -1, -1, RAWKEY,
  57.   ACTIVATE | WINDOWDEPTH | SMART_REFRESH,
  58.   NULL, NULL,
  59.   (UBYTE *) "Vmore  Copyright (C) 1987 By Stephen Vermeulen",
  60.   NULL,
  61.   NULL,
  62.   -1, -1, -1, -1,
  63.   WBENCHSCREEN
  64. };
  65.  
  66. /********************************************
  67.   The following structure is used to keep a
  68.   record of the positions to seek() to when
  69.   the page backward key is pressed.
  70. *********************************************/
  71.  
  72. struct PageStart
  73. {
  74.   struct PageStart *prev;   /* previous page */
  75.   long pos;                 /* position      */
  76. };
  77.  
  78. char s[90];
  79.  
  80. void main(argc, argv)
  81. short argc;
  82. char *argv[];
  83. {
  84.   short lib_flags, init_flags, pause;
  85.   short left, top, maxx, maxy, c, code;
  86.   FILE *in;
  87.   struct RastPort *rp;
  88.   struct Window *w;
  89.   struct IntuiMessage *mess;
  90.   struct PageStart *ps, *ps2;
  91.   struct WBArg *arg;
  92.   short nargs, CLI_flag, files_left;
  93.   char *name;
  94.   struct FileLock *start_dir;
  95.  
  96.   CLI_flag = argc;       /* true if run from CLI, false from WB */
  97.   lib_flags  = OpenLibs();
  98.   if (!lib_flags)
  99.   {
  100.     w = (struct Window *) OpenWindow(&nw);
  101.     if (w)
  102.     {
  103.       rp = w->RPort;
  104.       SetAPen(rp, 1L);
  105.       SetDrMd(rp, JAM1);
  106.       left = w->BorderLeft + 1;
  107.       top = w->BorderTop + rp->TxHeight;
  108.       maxx = w->Width  - w->BorderRight - 1;
  109.       maxy = w->Height - w->BorderBottom - 1;
  110.       if (CLI_flag)
  111.       {
  112.         --argc;
  113.         files_left = argc;
  114.         name = argv[argc];
  115.       }
  116.       else
  117.       {
  118.         nargs = WBenchMsg->sm_NumArgs;
  119.         arg = WBenchMsg->sm_ArgList;
  120.         start_dir = (struct FileLock *) CurrentDir(arg->wa_Lock);
  121.         /****** skip tool name *******/
  122.         ++arg;
  123.         files_left = --nargs;
  124.         (void) CurrentDir(arg->wa_Lock);
  125.         name = arg->wa_Name;
  126.       }
  127.       while(files_left)
  128.       {
  129.         in = fopen(name, "r");
  130.         if (in)
  131.         {
  132.           /**********************************
  133.             Allocate the root page start node
  134.           ***********************************/
  135.  
  136.           ps = (struct PageStart *) malloc(sizeof(struct PageStart));
  137.           ps->prev = NULL;
  138.           ps->pos = 0L;
  139.           SetAPen(rp, 0L);
  140.           RectFill(rp, (long) left, (long) w->BorderTop + 1L,
  141.                        (long) maxx, (long) maxy);
  142.           SetAPen(rp, 1L);
  143.           Move(rp, (long) left, (long) top);
  144. new_line:
  145.           while (fgets(s, 79, in))
  146.           {
  147.             /***************************************
  148.               remove the newline marker, if it is
  149.               present
  150.             ****************************************/
  151.  
  152.             s[79] = 0;
  153.             c = strlen(s);
  154.             if (s[c - 1] == 10)
  155.             {
  156.               --c;
  157.               s[c] = 0;
  158.             }
  159.  
  160.             /*****************************
  161.               Now check to see if too far
  162.               down the window.
  163.             ******************************/
  164.  
  165.             if (rp->cp_y >= maxy)
  166.             {
  167.               pause = TRUE;
  168.               while(pause)
  169.               {
  170.                 Wait(1L << w->UserPort->mp_SigBit);
  171.                 while(mess = (struct IntuiMessage *) GetMsg(w->UserPort))
  172.                 {
  173.                   code = mess->Code;
  174.                   ReplyMsg(mess);
  175.                   switch (code)
  176.                   {
  177.                     case 0x0c5: /* ESC key released */
  178.                       goto free_up;
  179.  
  180.                     case 192:   /* space bar released */
  181.                     case 0x0cd: /* down arrow key up  */
  182.                       pause = FALSE;
  183.                       SetAPen(rp, 0L);
  184.                       RectFill(rp, (long) left, (long) w->BorderTop + 1L,
  185.                                    (long) maxx, (long) maxy);
  186.                       SetAPen(rp, 1L);
  187.                       Move(rp, (long) left, (long) top);
  188.  
  189.                       /********************************
  190.                         Now allocate and link in a new
  191.                         page start structure.
  192.                       *********************************/
  193.  
  194.                       ps2 = (struct PageStart *)
  195.                             malloc(sizeof(struct PageStart));
  196.                       ps2->prev = ps;
  197.                       ps2->pos = ftell(in);
  198.                       ps = ps2;
  199.                       break;
  200.  
  201.                     case 0x0cc: /* up arrow key up */
  202.  
  203.                       SetAPen(rp, 0L);
  204.                       RectFill(rp, (long) left, (long) w->BorderTop + 1L,
  205.                                    (long) maxx, (long) maxy);
  206.                       SetAPen(rp, 1L);
  207.                       Move(rp, (long) left, (long) top);
  208.  
  209.                       /**********************************
  210.                         Deallocate the current pagestart
  211.                         node and move backwards in the
  212.                         file.
  213.                       ***********************************/
  214.  
  215.                       ps2 = ps->prev;
  216.                       if (ps2)
  217.                       {
  218.                         free(ps);
  219.                         ps = ps2;
  220.                         fseek(in, ps->pos, 0);
  221.                       }
  222.                       else
  223.                       {
  224.                         fseek(in, 0L, 0);
  225.                       }
  226.                       goto new_line;
  227.                   }
  228.                 }
  229.               }
  230.             }
  231.             Text(rp, s, (long) c);
  232.             Move(rp, (long) left, (long) (rp->cp_y + rp->TxHeight + 1) );
  233.           }
  234.           Wait(1L << w->UserPort->mp_SigBit);
  235. free_up:
  236.           while(mess = (struct IntuiMessage *) GetMsg(w->UserPort))
  237.             ReplyMsg(mess);
  238.         }
  239.  
  240.         /************************************
  241.           free up the page start list
  242.         *************************************/
  243.  
  244.         do
  245.         {
  246.           ps2 = ps->prev;
  247.           free(ps);
  248.           ps = ps2;
  249.         } while (ps);
  250.         fclose(in);
  251.         --files_left;
  252.         if (CLI_flag)
  253.           name = argv[files_left];
  254.         else
  255.         {
  256.           ++arg;
  257.           (void) CurrentDir(arg->wa_Lock);
  258.           name = arg->wa_Name;
  259.         }
  260.       }
  261.       CloseWindow(w);
  262.     }
  263.     if (!CLI_flag) (void) CurrentDir(start_dir);
  264.     CloseLibs(lib_flags);
  265.   }
  266. }
  267.